<template>
  <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :immediate-check="false"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
  >
    <!--    <van-cell v-for="item in list" :key="item.com_id+''" :title="item.content"/>-->
    <CommentItem @clickReply="$emit('clickReply',$event)" :row="item" v-for="item in list"
                 :key="item.com_id+''"></CommentItem>
  </van-list>
</template>

<script>
import { getCommentsList } from '@/api/article'
import CommentItem from '@/components/CommentItem'

export default {
  name: 'CommentList',
  components: {
    CommentItem
  },
  props: {
    type: {
      type: String,
      default: 'a'
    },
    source: {
      type: [Object, String, Number],
      required: true
    },
    list: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10,
      error: false
    }
  },
  created () {
    this.onLoad()
  },
  methods: {
    async onLoad () {
      try {
        const res = await getCommentsList({
          type: this.type,
          source: this.source + '',
          offset: this.offset,
          limit: this.limit
        })
        this.list.push(...res.results)
        console.log(res)
        this.$emit('updateTotalCount', res.total_count)
        this.loading = false
        if (!res.last_id || this.list.length >= res.total_count) {
          this.finished = true
        } else {
          this.offset = res.last_id
        }
      } catch (e) {
        this.loading = false
        this.error = true
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
